// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "CommonActivatableWidget.h"
#include "GameFeatureAction_WorldActionBase.h"
#include "UIExtensionSystem.h"

#include "GameFeatureAction_AddWidget.generated.h"

struct FWorldContext;
struct FComponentRequestHandle;

/**
 * HUD布局请求结构体
 */
USTRUCT()
struct FLyraHUDLayoutRequest
{
	GENERATED_BODY()

	// 要生成的布局控件
	UPROPERTY(EditAnywhere, Category=UI, meta=(AssetBundles="Client"))
	TSoftClassPtr<UCommonActivatableWidget> LayoutClass; // 布局类

	// 要插入控件的层
	UPROPERTY(EditAnywhere, Category=UI, meta=(Categories="UI.Layer"))
	FGameplayTag LayerID; // 层ID
};


/**
 * HUD元素条目结构体
 */
USTRUCT()
struct FLyraHUDElementEntry
{
	GENERATED_BODY()

	// 要生成的控件
	UPROPERTY(EditAnywhere, Category=UI, meta=(AssetBundles="Client"))
	TSoftClassPtr<UUserWidget> WidgetClass; // 控件类

	// 应该放置此控件的插槽ID
	UPROPERTY(EditAnywhere, Category = UI)
	FGameplayTag SlotID; // 插槽ID
};

//////////////////////////////////////////////////////////////////////
// UGameFeatureAction_AddWidget

/**
 * 负责添加控件的GameFeatureAction
 */
UCLASS(MinimalAPI, meta = (DisplayName = "Add Widgets"))
class UGameFeatureAction_AddWidgets final : public UGameFeatureAction_WorldActionBase
{
	GENERATED_BODY()

public:
	//~ 开始 UGameFeatureAction 接口
	virtual void OnGameFeatureDeactivating(FGameFeatureDeactivatingContext& Context) override; // 游戏特性停用时调用
#if WITH_EDITORONLY_DATA
	virtual void AddAdditionalAssetBundleData(FAssetBundleData& AssetBundleData) override; // 添加额外的资源包数据
#endif
	//~ 结束 UGameFeatureAction 接口

	//~ 开始 UObject 接口
#if WITH_EDITOR
	virtual EDataValidationResult IsDataValid(class FDataValidationContext& Context) const override; // 数据验证
#endif
	//~ 结束 UObject 接口

private:
	// 要添加到HUD的布局
	UPROPERTY(EditAnywhere, Category=UI, meta=(TitleProperty="{LayerID} -> {LayoutClass}"))
	TArray<FLyraHUDLayoutRequest> Layout; // 布局列表

	// 要添加到HUD的控件
	UPROPERTY(EditAnywhere, Category=UI, meta=(TitleProperty="{SlotID} -> {WidgetClass}"))
	TArray<FLyraHUDElementEntry> Widgets; // 控件列表

private:

	/**
	 * 每个Actor的数据结构
	 */
	struct FPerActorData
	{
		TArray<TWeakObjectPtr<UCommonActivatableWidget>> LayoutsAdded; // 已添加的布局数组
		TArray<FUIExtensionHandle> ExtensionHandles; // 扩展句柄数组
	};

	/**
	 * 每个上下文的数据结构
	 */
	struct FPerContextData
	{
		TArray<TSharedPtr<FComponentRequestHandle>> ComponentRequests; // 组件请求数组
		TMap<FObjectKey, FPerActorData> ActorData;  // Actor数据映射
	};

	TMap<FGameFeatureStateChangeContext, FPerContextData> ContextData; // 上下文数据映射

	//~ 开始 UGameFeatureAction_WorldActionBase 接口
	virtual void AddToWorld(const FWorldContext& WorldContext, const FGameFeatureStateChangeContext& ChangeContext) override; // 添加到世界
	//~ 结束 UGameFeatureAction_WorldActionBase 接口

	void Reset(FPerContextData& ActiveData); // 重置数据

	void HandleActorExtension(AActor* Actor, FName EventName, FGameFeatureStateChangeContext ChangeContext); // 处理Actor扩展

	void AddWidgets(AActor* Actor, FPerContextData& ActiveData); // 添加控件
	void RemoveWidgets(AActor* Actor, FPerContextData& ActiveData); // 移除控件
};